<?php
/**
 * @author huwhois<huwhois@163.com>
 */
namespace app\admin\controller;

use daswork\Controller;
use app\admin\model\User as UserModel;

class User extends Base
{   
    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new UserModel();
    }

    public function index()
    {
        $data = $this->model->dataList();
        $this->assign('data', $data);
        $this->fetch();
    }

    public function info()
    {
        $id = $_GET['id'];
        if (!$id) {
            echo json_encode(['code' => 1, 'msg'=>'id不能为空']);
            return false;
        }
        $data = $this->model->getOneById($id);
        echo json_encode(['code'=>0, 'data'=>$data]);
        return false;
    }

    public function save()
    {
        $params = escapeString($_POST);
        $data = [
            'truename' => $params['truename'],
            'organization' => $params['organization'],
            'phone' => $params['phone'],
            'fee_type' => intval($params['feeType']), 
            'is_pay' => intval($params['isPay']),
            'money' =>intval($params['money']),
            'remark' => $params['remark'],
            'member_type'=> intval($params['memberType']),
            'receipt_number'=> intval($params['receipt_number'])
        ];
        
        $id = $params['id'];
        if ($id) {
            $data['id'] = $id;
            $res = $this->model->updateById($data);
        } else {
            $res = $this->model->save($data);
        }

        if (!$res) {
            echo json_encode(['code' => 2, 'msg'=>$this->model->lastErrorMsg()]);
            return false;
        } else {
            echo json_encode(['code' => 0]);
            return false;
        }
    }

    public function delete()
    {
        $id = escapeString($_POST['id']);
        if (!$id) {
            echo json_encode(['code' => 1, 'msg'=>'id不能为空']);
            return false;
        }
        if (is_array($id)) {
            if (in_array(1, $id)) {
                echo json_encode(['code' => 1, 'msg'=>'超级管理员不可删除']);
                return false;
            }
        } elseif ($id == 1) {
            echo json_encode(['code' => 1, 'msg'=>'超级管理员不可删除']);
            return false;
        }
        
        $res = $this->model->deleteById($id);
        if (!$res) {
            echo json_encode(['code' => 2, 'msg'=>$this->model->lastErrorMsg()]);
            return false;
        } else {
            echo json_encode(['code' => 0]);
            return false;
        }
    }

    public function repassword()
    {
        $params = escapeString($_POST);

        $id = $_SESSION['userid'];
        $user = $this->model->getOneById($id);
        if ($user['password'] != md5($params['oldpassword'])) {
            echo json_encode(['code' => 2, 'msg'=>'原密码不正确']);
            return false;
        }

        $user['password'] = md5($params['newpassword']);

        $res = $this->model->updateById($user);

        echo json_encode(['code' => 0, 'msg'=>'修改成功, 请重新登录']);
        return false;
    }
}
